package com.application.lucky.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.bean.jsonBean.HistoryBean;
import com.application.lucky.bean.jsonBean.RedPListbean;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.request.nav.UrlManager;
import com.application.lucky.utils.CTextUtils;
import com.qunar.im.ui.util.MoneyUtils;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.DateUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qunar.im.ui.util.ProfileUtils;

import java.util.List;

public class RedPHistoryActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private RecyclerView mRclView;
    private MyAdapter myAdapter;
    private ImageView mIvHeadView;
    private TextView mTvName;
    private TextView mTvMoney;
    private TextView mTvNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    @Override
    public void beforeInit() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_red_phistory;
    }

    private void initView() {
        mTitleBar = findViewById(R.id.title_bar);
        mRclView = findViewById(R.id.rcl_view);

        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });
        mRclView.setLayoutManager(new LinearLayoutManager(this));
        myAdapter = new MyAdapter(R.layout.item_red_history);
        mRclView.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RedPListbean item = myAdapter.getItem(position);
                Intent intent = new Intent(RedPHistoryActivity.this, RedPContentActivity.class);
                intent.putExtra("data", item);
                startActivityAnim(intent);

            }
        });
        myAdapter.addHeaderView(getHeadView());
        myAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadData();
            }
        }, mRclView);
        loadData();

    }

    int index = 0;

    private void loadData() {
        new RedPRequest().red_history(index, new CallBackEx<BaseResponse<HistoryBean>>() {
            @Override
            public void onFailed(int code) {

            }

            @Override
            public void onSuccess(BaseResponse<HistoryBean> baseResponse) {

                HistoryBean dataInfo = baseResponse.dataInfo;
                if (dataInfo != null) {

                    mTvNum.setText(CTextUtils.getBuilder("发出红包总数")
                            .append(dataInfo.getTotal() + "").setForegroundColor(Color.parseColor("#C79B26"))
                            .append("个").create());
                    List<RedPListbean> list = dataInfo.getList();
                    if (list != null) {

                        if (index == 0) {
                            myAdapter.setNewData(list);
                        } else {
                            myAdapter.addData(list);
                        }
                        index += list.size();
                        if (list.size() < 10) {
                            myAdapter.loadMoreEnd(true);
                        } else {
                            myAdapter.loadMoreComplete();
                        }
                    }

                }
            }
        });
    }

    private View getHeadView() {
        View view = LayoutInflater.from(this).inflate(R.layout.view_history_head, null);
        mIvHeadView = (ImageView) view.findViewById(R.id.iv_head_view);
        ProfileUtils.displayGravatarRoundedCornerByImageSrc(this, UrlManager.getHeadPic(UserManagers.getInstance().getPic()), mIvHeadView);
        mTvName = (TextView) view.findViewById(R.id.tv_name);
        mTvName.setText(UserManagers.getInstance().getUserName());
        mTvMoney = (TextView) view.findViewById(R.id.tv_money);
        mTvNum = (TextView) view.findViewById(R.id.tv_num);
        return view;
    }

    static class MyAdapter extends BaseQuickAdapter<RedPListbean, BaseViewHolder> {
        public MyAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(BaseViewHolder helper, RedPListbean item) {
            ImageView view = helper.getView(R.id.iv_red_p_list_head);
            ProfileUtils.displayGravatarRoundedCornerByImageSrc(((Activity) mContext), UrlManager.getHeadPic(item.getUser().getHeadPic()), view);
            helper.setText(R.id.iv_red_p_list_time, DateUtils.getRedTime(item.getCreateTime()))
                    .setText(R.id.iv_red_p_list_name, item.getUser().getNickName())
                    .setText(R.id.iv_red_p_list_money, "¥" + MoneyUtils.getMoney(item.getMoney()));
        }
    }
}
